package com.io.file;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 从键盘输入一串字符，直到输入回车符结束，
 * 然后从屏幕输出并将其存入a.txt文件中。提示：
 * 输入：System.in.read();输出：System.out.write();
 *
 * @author: Jovi
 * @createTime: 2017-06-09 14:40
 **/
public class InFile {
    public static void main(String[] args) {
        char ch;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("a.txt");
            System.out.println("Please enter(A carriage return end)：");
            while ((ch = (char)System.in.read()) != '\n') {
                System.out.write(ch);
                fos.write(ch);
            }
            System.out.println(" Enter over!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
